#include "Vec3f.h"

Vec3f::Vec3f(GLfloat v0, GLfloat v1, GLfloat v2) 
{
	V[0] = v0;
	V[1] = v1;
	V[2] = v2;
}
Vec3f::Vec3f(const Vec3f& v) {*this = v; }

bool Vec3f::ReadFromString(const char* line)
{
	if (!line) return false;
	GLfloat v[3];
	if (sscanf(line, "%f,%f,%f", &v[0], &v[1], &v[2]) != 3)
		return false;
	V[0] = v[0];
	V[1] = v[1];
	V[2] = v[2];
	return true;
}

GLfloat Vec3f::operator[] (unsigned index) const {return V[index];}
GLfloat& Vec3f::operator[] (unsigned index) {return V[index];}

const Vec3f& Vec3f::operator= (const Vec3f& v)
{
	V[0] = v[0];
	V[1] = v[1];
	V[2] = v[2];
	return *this;
}

bool Vec3f::operator== (const Vec3f& v) const
{
	return (fEquals(V[0], v[0]) && fEquals(V[1], v[1]) && fEquals(V[2], v[2]));
}


bool Vec3f::operator!= (const Vec3f& v) const
{
	return !(*this == v);
}

Vec3f Vec3f::operator+ (const Vec3f& rhs) const
{
	return Vec3f(V[0]+rhs[0], V[1]+rhs[1], V[2]+rhs[2]);
}

Vec3f Vec3f::operator- (const Vec3f& rhs) const
{
	return Vec3f(V[0]-rhs[0], V[1]-rhs[1], V[2]-rhs[2]);
}

// Vector DOT product
GLfloat Vec3f::operator* (const Vec3f& rhs) const
{
	return (V[0]*rhs[0] + V[1]*rhs[1] + V[2]*rhs[2]);
}

Vec3f Vec3f::operator* (GLfloat rhs) const
{
	return Vec3f(V[0]*rhs, V[1]*rhs, V[2]*rhs);
}

Vec3f Vec3f::operator/ (GLfloat rhs) const
{
	return *this * (1.0f / rhs);
}

GLfloat Vec3f::length() const
{
	return sqrt(V[0]*V[0] + V[1]*V[1] + V[2]*V[2]);
}

Vec3f Vec3f::Normalize() const
{
	GLfloat l = this->length();
	if (l == 0.0) return *this;
	else return *this / l;
}

Vec3f Vec3f::operator^ (const Vec3f& v) const
{
	return Vec3f(
                V[1] * v.V[2] - V[2] * v.V[1],
                V[2] * v.V[0] - V[0] * v.V[2],
                V[0] * v.V[1] - V[1] * v.V[0]
                );
}
